<?php
/**
 *  @Author  $: 
 *   @Id $: 
 */            
class My_Log{
    
    /*
    * Write log to db
    * $type: payment | (write to table $type)
    * $type: gerneral | write to table gereral
    * $data:
    */
	public static function writeLogStash($option = array())
	{
		try{
            $fileName = $_SERVER['SERVER_ADDR'] . '_' . $_SERVER['HTTP_HOST'] . '.log';
			$logPath = is_dir('/var/log/runtime_error') ? '/var/log/runtime_error/' . $fileName : WEB_PATH . "/log/logStash/logStash.txt";
            if(!is_file($logPath)){
                $h = fopen($logPath, 'w');
                fclose($h);
                chmod($logPath, 0775);
            }
			
			$trace = debug_backtrace();
	        $caller = array_shift($trace);
			$referer = isset($_SERVER['HTTP_REFERER']) ? " Referer: " . $_SERVER['HTTP_REFERER'] : " Referer: Null";
			$IPClient = isset($_SERVER['HTTP_X_REAL_IP']) ? $_SERVER['HTTP_X_REAL_IP'] : $_SERVER['REMOTE_ADDR'];
			$errorMessage = isset($option['message']) ? str_replace (array("\r\n", "\n", "\r"), '', $option['message']) : 'Null';
			$errorCode = isset($option['code']) ? $option['code'] : 'Null';
			$message = "Time: " . date('d/m/Y H:i:s', time()) . 
						" IPClient: " . $IPClient . 
						" IPServer: " . $_SERVER['SERVER_ADDR'] .
						" Message: " . $errorMessage. 
						" ErrorCode: " . $errorCode . 
						" File: " . $caller['file'] . 
						" Line: " . $caller['line'] . 
						" Host: " . $_SERVER['HTTP_HOST'] . 
						" Request Uri: " . $_SERVER['REQUEST_URI'] . $referer . 
						" FileCatch: " . __FILE__ . 
						" LineCatch: " . __LINE__;
			file_put_contents($logPath, $message . "\n", FILE_APPEND);
		}catch(Exception $e){}
	}
                              
}
